//
//  CategoryListWaterflowLayout.m
//  FlyGift
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CategoryListWaterflowLayout.h"

///**
// *  每一列的间距
// */
//static const CGFloat columnMagin = 10;
///**
// *  每一行的间距
// */
//static const CGFloat rowMagin = 10;



@interface CategoryListWaterflowLayout ()

/***  这个字典用来存储每一列最大的Y值(每一列的高度)*/
@property(nonatomic, strong)NSMutableDictionary *maxYDict;
// key值是列(是字符串类型) values对应的是当前列的最短的y值(存放NSnumber类型)

@property(nonatomic, strong)NSMutableArray *attrsArray;
@end

@implementation CategoryListWaterflowLayout

#pragma mark - 懒加载
- (NSMutableDictionary *)maxYDict
{
    if (!_maxYDict) {
        self.maxYDict = [[NSMutableDictionary alloc] init];
    }
    return _maxYDict;
}

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        self.attrsArray = [[NSMutableArray alloc] init];
    }
    return _attrsArray;
}



- (instancetype)init
{
    self = [super init];
    if (self) {
        self.columnMagin = 10;
        self.rowMagin = 10;
        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.columnsCount = 2; // 默认显示2列
        
    }
    return self;
}

/**
 *  发生滚动 边界改变的时候
 */
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

/**
 *  每次布局之前的准备
 */

- (void)prepareLayout
{
    [super prepareLayout];
    
    // 清空最大值
    for (int i = 0; i < self.columnsCount; i++)
    {
        NSString *column = [NSString stringWithFormat:@"%d",i];
        self.maxYDict[column] = @(self.sectionInset.top);
    }
    
    // 计算所有的cell的属性
    [self.attrsArray removeAllObjects];
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i<count; i++) {
        // 根据indexpath知道layout布局
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [self.attrsArray addObject:attrs];
    }

    
    
}

/**
 *  返回所有的尺寸
 *
 */
- (CGSize)collectionViewContentSize
{
    __block NSString *maxColumn = @"0";
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString * column, NSNumber *maxY, BOOL *stop) {
        if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue])
        {
            maxColumn = column;
        }
    }];
    return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom);
}


/**
 *  返回indexpath这个位置的Item的布局属性 需要先知道每一列的y值 需要时刻记录y值
 *
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 假设最短的那一列是第0列
   __block NSString *minColumn = @"0";
    // 挑选出最短的Y值 遍历该字典
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString * column, NSNumber *maxY, BOOL *stop) {
        // 存放的是NSNumber类型 需要转换成float类型
        // 如果遍历到的最大的Y值比(假设的这个最短列还小)
        if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue])
        {
            minColumn = column;
        }
    }];
    
    // 计算宽度 (利用collectionView的宽度 减去 左边间距 以及右边间距 再减去中间的列间距 再除以2)
    CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMagin) / self.columnsCount;
    // 计算高度
//    CGFloat height = 100 + arc4random_uniform(100); // 100 -200 的随机数
    CGFloat height = [self.delegate categoryListWaterflowLayout:self heightForWidth:width atIndexpath:indexPath];
    
    // 计算位置
    CGFloat x = self.sectionInset.left + (width + self.columnMagin) * ([minColumn intValue]);
    CGFloat y = [self.maxYDict[minColumn] floatValue] + self.rowMagin;
    
    // 更新这一列的最大Y值
    self.maxYDict[minColumn] = @(y + height);
    
    // 创建属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes  layoutAttributesForCellWithIndexPath:indexPath];
    attrs.frame = CGRectMake(x, y, width, height);
    
    return attrs;
}


/**
 *  返回rect范围内的布局属性 根据有多少张图片生成多少个indexPath
 *
 */
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attrsArray;
}
@end












